/* ********************************
 * @Author: zhengshizeng@126.com
 * @Date: 2024-08-02 15:09:54
 * ********************************/

#ifndef __cserverManz_H__Z_
#define __cserverManz_H__Z_

#include "Http/HttpRequester.h"
#include "Http/WebSocketSession.h"
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Poller/Timer.h"
#include "Util/NoticeCenter.h"
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "cjsonconfigz.h"
#include "json/json.h"
#include <chrono>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <signal.h>
#include <string>

using namespace std;
using namespace toolkit;
using namespace mediakit;

class cserverManz : public std::enable_shared_from_this<cserverManz> {
public:
    using Ptr = std::shared_ptr<cserverManz>;
    cserverManz();
    ~cserverManz() = default;

private:
    using HttpApiz = function<int(const Json::Value &args,Json::Value &rval)>;
    // http api列表
    map<string, HttpApiz> m_map_api;

    string m_rtsp_key; // 用来关闭流
    string m_mediaSecret; // ZLM Key
    string m_mediaURL;
    string m_local_ip;
    EventPoller::Ptr m_poller;
    // 保存要注册流的URL
    std::map<std::string, std::string> m_URLdataMap;
    std::mutex m_mutexURL;

private:
    // 生成键的函数
    string generateKey(const std::string &app, const std::string &stream);
    void OnInitAPIFUN();

    int HandleAddStream(const Json::Value &args, Json::Value &rval);
    int HandleStreamNotFound(const Json::Value &args, Json::Value &rval);
    int HandleAddPlayback(const Json::Value &args, Json::Value &rval);
    
public:
    bool OnInit();
    void UnInit();
    void OnStart();
    bool OnaddStreamProxy(const string &app, const string &stream, const string sUrl);
    bool OndelStreamProxy();
    string GetUrlByKey(const string &app, const string &stream);
    void SetUrlByKey(const string &app, const string &stream, const string &url);
    void OnInitListener();
};

#endif // __cserverManz_H__Z_